package org.vacoor.xqq.ui.chat;

import org.vacoor.xqq.ui.util.UIs;
import org.vacoor.xqq.ui.comp.border.RolloverBorder;
import org.vacoor.xqq.ui.dynamictree.Buddy;
import org.vacoor.xqq.ui.util.ImageUtil;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * User: Vacoor
 */
public class AvatarPanel extends JPanel {
    public static final Font BEST_FONT = UIs.getBestFont();
//    public static final Dimension AVATAR_SIZE = new Dimension(40, 40);
    public static final Dimension AVATAR_SIZE = new Dimension(30, 30);
    public static final Color PRIMARY_COLOR = Color.BLACK;
    public static final Color SECONDARY_COLOR = new Color(0x7F7F7F);

    private DataChangeMonitor monitor = new DataChangeMonitor();

    private JLabel avatar;
    private JLabel nick;
    private JLabel mark;
    private JLabel additional;
    private Buddy buddy;

    AvatarPanel() {
        init();
    }

    public AvatarPanel(Buddy buddy) {
        this.buddy = buddy;
        init();
        initDataAndRenderer(buddy);
    }

    private void init() {
        this.avatar = new JLabel();
        this.nick = new JLabel();
        this.mark = new JLabel();
        this.additional = new JLabel();

        this.setLayout(new GridBagLayout());
        this.setOpaque(false);
        this.setBorder(null);

        avatar.setOpaque(false);
        nick.setOpaque(false);
        mark.setOpaque(false);
        additional.setOpaque(false);

        nick.setForeground(PRIMARY_COLOR);
        mark.setForeground(PRIMARY_COLOR);
//        nick.setForeground(SECONDARY_COLOR);
//        mark.setForeground(SECONDARY_COLOR);
        additional.setForeground(SECONDARY_COLOR);

        //
        nick.setFont(BEST_FONT);
        mark.setFont(BEST_FONT);
        additional.setFont(BEST_FONT.deriveFont(BEST_FONT.getSize() - 2));

        avatar.setBorder(
                new RolloverBorder(
                        BorderFactory.createCompoundBorder(
                                BorderFactory.createLineBorder(new Color(0xCCCCCC), 1),
                                BorderFactory.createEmptyBorder(1, 1, 1, 1)
                        ),
                        BorderFactory.createCompoundBorder(
                                new LineBorder(new Color(96, 200, 253), 1, true),
                                new LineBorder(new Color(51, 139, 192), 1, true)
                        )
                )
        );
    }

    void initDataAndRenderer(Buddy buddy) {
        if(this.buddy != null) {
            buddy.removePropertyChangeListener(monitor);
        }
        this.buddy = buddy;

        if(buddy != null) {
            buddy.addPropertyChangeListener(monitor);
            setAvatarIcon(buddy.getAvatar());
            setNickText(buddy.getNick());
            setMarkText(buddy.getMark());
            setAdditionalText(buddy.getAdditional());
        }

        renderer();
    }

    private void renderer() {
        nick.setForeground(PRIMARY_COLOR);

        this.add(avatar, new GridBagConstraints(0, 0, 1, 2, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 0, 1, 5), 0, 0));
        if (!markIsEmpty()) {
            this.add(mark, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.CENTER, new Insets(0, 5, 1, -4), 0, 0));
            nick.setForeground(SECONDARY_COLOR);
        }
        this.add(nick, new GridBagConstraints(2, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.CENTER, new Insets(0, 5, 1, 2), 0, 0));
        this.add(additional, new GridBagConstraints(1, 1, 2, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.CENTER, new Insets(1, 5, 1, 2), 0, 0));
    }

    private boolean markIsEmpty() {
        return mark.getText() == null || mark.getText().length() < 1;
    }

    public void setAvatarIcon(Icon icon) {
        if(icon != null) {
            Image resize = ImageUtil.resize(((ImageIcon) icon).getImage(), AVATAR_SIZE);
            icon = new ImageIcon(resize);
        }
        avatar.setIcon(icon);
    }

    public Icon getAvatarIcon() {
        return avatar.getIcon();
    }

    public void setNickText(String nick) {
        this.nick.setText(markIsEmpty() ? nick : "(" + nick + ")");
    }

    public String getNickText() {
        return nick.getText();
    }

    public void setMarkText(String mark) {
        this.mark.setText(mark);
        String n = this.nick.getText();
        if (n != null) {
            n = n.replaceAll("^\\(|\\)$", "");
            this.nick.setText(markIsEmpty() ? n : "(" + n + ")");
        }
    }

    public String getMarkText() {
        return mark.getText();
    }

    public void setAdditionalText(String additional) {
        this.additional.setText(additional);
    }

    public String getAdditionalText() {
        return additional.getText();
    }

    private class DataChangeMonitor implements PropertyChangeListener {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            initDataAndRenderer(buddy);
        }
    }
}
